package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import pojo.Food;
import pojo.User;
import util.DBUtil;
import util.DBUtil.ConnType;
import dao.UserDao;

public class UserDaoImpl implements UserDao{
	
	private static ConnType ConnectType = ConnType.JNDI_ORACLE;

	@Override
	public int addUser(User u) {
		Connection con = null;
		PreparedStatement pst = null;
		String sql = "insert into users(id, name, psd) values(?,?,?)";
		int m = 0;
		try {
			con = DBUtil.createConnection(ConnectType);
			pst = con.prepareStatement(sql);
			
			pst.setString(1, u.getId());
			pst.setString(2, u.getName());
			pst.setString(3, u.getPassword());

			m = pst.executeUpdate(); 
		} catch (Exception e) {
			 
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	@Override
	public int deleteUser(String id) {
		Connection con = null;
		PreparedStatement pst = null;
		String sql = "delete from users where id = ?";
		int m = 0;
		try {
			con = DBUtil.createConnection(ConnectType);
			pst = con.prepareStatement(sql);
			
			pst.setString(1, id);

			m = pst.executeUpdate(); 
		} catch (Exception e) {
			 
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	@Override
	public int updateUser(User u) {
		Connection con = null;
		PreparedStatement pst = null;
		String sql = "update users set name=?, psd = ? where id=?";
		int m = 0;
		try {
			con = DBUtil.createConnection(ConnectType);
			pst = con.prepareStatement(sql);
			
			
			pst.setString(1, u.getName());
			pst.setString(2, u.getPassword());
			pst.setString(3, u.getId());

			m = pst.executeUpdate(); 
		} catch (Exception e) {
			 
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	@Override
	public User loadUserByName(String name) {
		Connection con = null;
  		PreparedStatement pst = null;
  		ResultSet rs = null;
  		
  		String sql = "select * from users where name =?";

  		User u = null;
  		try {
  			con = DBUtil.createConnection(ConnectType);
			pst = con.prepareStatement(sql);
			pst.setString(1, name);
			
			rs = pst.executeQuery();
			
			if(rs.next()){
				u = new User();
				u.setId(rs.getString("id"));
				u.setName(rs.getString("name"));
				u.setPassword(rs.getString("psd"));	
			}
		} catch (Exception e) {
	 
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		} 
  		return u;	
	}

	@Override
	public User loadUserById(String id) {
		Connection con = null;
  		PreparedStatement pst = null;
  		ResultSet rs = null;
  		
  		String sql = "select * from users where id =?";

  		User u = null;
  		try {
  			con = DBUtil.createConnection(ConnectType);
			pst = con.prepareStatement(sql);
			pst.setString(1, id);
			
			rs = pst.executeQuery();
			
			if(rs.next()){
				u = new User();
				u.setId(rs.getString("id"));
				u.setName(rs.getString("name"));
				u.setPassword(rs.getString("psd"));	
			}
		} catch (Exception e) {
	 
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		} 
  		return u;
	}

	@Override
	public List<User> findAllUsers() {
		List<User> list = new ArrayList<User>();
		Connection con = null;
  		PreparedStatement pst = null;
  		ResultSet rs = null;
  		
  		String sql = "select * from users";

  		User u = null;
  		try {
  			con = DBUtil.createConnection(ConnectType);
			pst = con.prepareStatement(sql);
			
			rs = pst.executeQuery();
			
			while(rs.next()){
				u = new User();
				u.setId(rs.getString("id"));
				u.setName(rs.getString("name"));
				u.setPassword(rs.getString("psd"));
				list.add(u);
			}
		} catch (Exception e) {
	 
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		} 
  		
  		return list;
	}

}
